/* $$$ JS file: nDynamic/nWYSIWIG.js: */
function nWYSIWIG(obj) {
	
	if (document.selection) {
		obj.bound = function(prefix, sufix) {
			this.focus();
			var str = document.selection.createRange().text;
			var sel = document.selection.createRange();
			sel.text = prefix + str + sufix;
			this.focus();
			return false;
		}
		
		obj.insert = function(content) {
			this.focus();
			document.selection.createRange().text = content;
			this.focus();
			return false;
		}
	} else {
		obj.bound = function(prefix, sufix) {
			this.focus();
			startPos = this.selectionStart;
			endPos = this.selectionEnd;
			before = this.value.substr(0, startPos);
			selected = this.value.substr(this.selectionStart, (this.selectionEnd - this.selectionStart));
			after = this.value.substr(this.selectionEnd, (this.value.length - this.selectionEnd));
			this.value = before + prefix + selected + sufix + after;
			return false;
		}
		
		obj.insert = function(content) {
			this.focus();
			startPos = this.selectionStart;
			endPos = this.selectionEnd;
			before = this.value.substr(0, startPos);
			selected = this.value.substr(this.selectionStart, (this.selectionEnd - this.selectionStart));
			after = this.value.substr(this.selectionEnd, (this.value.length - this.selectionEnd));
			this.value = before + content + after;
			return false;
		}
	}
	
	
	obj.bold = function() {
		return obj.bound('<b>', '</b>');
	}
	
	obj.insertColumnBreak = function() {
		return obj.insert('\n{COL_BREAK}\n');
	}
	
	obj.boundColumns = function() {
		return obj.bound('\n{COL_START}\n', '\n{COL_END}\n');
	}
	
	obj.insertImage = function() {
		return obj.bound('{IMAGE: ', '}');
	}
}
/* $$$ JS file: nDynamic/nComboDepended.js: */
function nComboDepended(obj, slave, slaveOptions) {
	var i, j, k;
	obj.slave = slave;
	obj.slaveOptions = slaveOptions;
	

	for (i = obj.options.length - 1; i >= 0; i--) {
		obj.options[i].slaveOptions = obj.slaveOptions[i];
		for (j = 0; j < obj.slaveOptions[i].length; j++) {
			for (k in obj.slaveOptions[i][j]) {
				obj.slaveOptions[i][j] = new Option(k, obj.slaveOptions[i][j][k]);
			}
		}
	}
	
	obj.slave.update = function() {
		var selected = obj.options[obj.selectedIndex];
		
		for (i = obj.slave.length - 1; i >= 0; i--) {
			obj.slave.remove(i);
		}
		
		if (selected.slaveOptions && selected.slaveOptions.length) {
			for (i in selected.slaveOptions) {
				if (nConfig.isIE) {
					obj.slave.add(selected.slaveOptions[i]);
				} else {
					obj.slave.add(selected.slaveOptions[i], null);
				}
			}
			obj.slave.selectedIndex = 0;
		}
		
		obj.slave.originalBackgroundColor = obj.slave.style.backgroundColor;
		obj.slave.originalColor = obj.slave.style.color;
		obj.slave.style.backgroundColor = '#000';
		obj.slave.style.color = '#fff';
		
		setTimeout(function() {
			obj.slave.style.backgroundColor = obj.slave.originalBackgroundColor;
			obj.slave.style.color = obj.slave.originalColor;
		}, 300);
	}

	nConfig.addEvent(obj, 'change', obj.slave.update);
//	obj.slave.update();
}
/* $$$ JS file: nDynamic/nSlideshow.js: */
function nSlideshow(obj) {
	var i, j;
	obj.images = $$(null, 'div', obj);
	
	if (obj.images.length > 1) {
		obj.currentImage = 0;
	
		j = 0;
		for (i in obj.images) {
			nSlideshowImage(obj.images[i], {hide: (j < obj.images.length - 1)});
			j++;
		}
		
		obj.next = function() {
			obj.open((obj.currentImage + 1) % obj.images.length);
		}
		
		obj.switchLayer = function(i) {
			obj.currentImage = i;
			obj.images[i].style.zIndex = 3;
			obj.images[i].show(function() {
//				if (i == obj.currentImage) {
					obj.hideOtherImages(obj.currentImage);
					obj.images[obj.currentImage].style.zIndex = 2;
//				}
			});
		}
		
		obj.open = function(i) {
			obj.currentImage = i;
			obj.switchLayer(i);
		}
		
		obj.hideOtherImages = function(focusImageNum) {
			var i;
			for (i in obj.images) {
				if (focusImageNum != i) {
					obj.images[i].hide();
					obj.images[i].style.zIndex = 1;
				}
			}
		}
		
		
		obj.loop = null;
		obj.runLoop = function() {
			if (obj.loop) {
				clearTimeout(obj.loop);
				obj.next();
			}
			obj.loop = setTimeout(obj.runLoop, 4000);
		}
		
//		nConfig.addEvent(obj, 'click', function() {
//			obj.runLoop();
//		});
		
		obj.runLoop();
	}
}


function nSlideshowImage(obj, options) {

	nDynamic(obj);
	obj.nName = 'nSlideshowImage';
	
	obj.configure(options);

	if (options.hide) {
		obj.cssOpacity(0);
	}

	obj.fader = obj.motion({
		factor: 0.03,
		n: 0,
		onUpdate:function() {
			this.obj.cssOpacity(this.n);
//			this.obj.style.marginLeft = this.n + 'px';
		}
	});

	obj.show = function(then) {
		obj.fader.activate(100).then(function() {
			then();
		});
	}

	obj.hide = function() {
		obj.fader.activate(0);
	}
}
/* $$$ JS file: ajax/aim.js: */
/**
*
*  AJAX IFRAME METHOD (AIM)
*  http://www.webtoolkit.info/
*
**/
AIM = {

	frame : function(c) {

		var n = 'f' + Math.floor(Math.random() * 99999);
		var d = document.createElement('DIV');
		d.innerHTML = '<iframe style="display:none" src="about:blank" id="'+n+'" name="'+n+'" onload="AIM.loaded(\''+n+'\')"></iframe>';
		document.body.appendChild(d);

		var i = document.getElementById(n);
		if (c && typeof(c.onComplete) == 'function') {
			i.onComplete = c.onComplete;
		}

		return n;
	},

	form : function(f, name) {
		f.setAttribute('target', name);
	},

	submit : function(f, c) {
		AIM.form(f, AIM.frame(c));
		if (c && typeof(c.onStart) == 'function') {
			return c.onStart();
		} else {
			return true;
		}
	},

	loaded : function(id) {
		var i = document.getElementById(id);
		if (i.contentDocument) {
			var d = i.contentDocument;
		} else if (i.contentWindow) {
			var d = i.contentWindow.document;
		} else {
			var d = window.frames[id].document;
		}
		
		if (d.location.href == "about:blank") {
			return;
		}

		if (typeof(i.onComplete) == 'function') {
			i.onComplete(d.body.innerHTML);
		}
	}

}
/* $$$ JS file: forms.js: */
function deleteImage(image, temporaryImage, wrapperObj) {
	if (confirm('Are you sure that you want to delete this image?\n')) {
		ajax.request('/image/' + (temporaryImage? 'ajaxTempDelete' : 'ajaxDelete') + '/' + image + '/', {
			loaderTo: wrapperObj,
			onSuccess:function(request) {
				if (request.responseText.substring(0, 2) == 'ok') {
					wrapperObj.parentNode.removeChild(wrapperObj);
				}
//				alert(request.responseText);
			},
			onPhpError:function(request) {
				alert(request.phpError);
			},
			onError:function(request) {
				alert(request.status);
			}
		});
	}

	return false;
}


function addTempImage(formObj, url, update) {
// formObj.addImageButton.value=formObj.addImageButton.name
	return ajax.submit(formObj, {
		url: url,
		update: update,
		uploadImages: true,
		onComplete:function(request) {
//			alert('Error1!\nHTTP code: ' + request.status + '\nResponse: ' + request.responseText);
			if (typeof(tb_init) == 'function') {
				tb_init('a.thickbox');
			}
		}//,
//		onError:function(request) {
//			alert('Error2!\nHTTP code: ' + request.status + '\nResponse: ' + request.responseText);
//		},
//		onPhpError:function(request) {
//			alert('Error3!\nHTTP code: ' + request.status + '\nResponse: ' + request.responseText + '\n\nphpError: ' + request.phpError);
//		}
	});
}


function removeEmptyFields(obj) {
	var emptyValueMask = new RegExp(/^\s*$/);

	var elems = $$(null, 'input', obj);
	for (i in elems) {
		if (emptyValueMask.test(elems[i].value)) {
			elems[i].disabled = 'disabled';
		}
	}
	var elems = $$(null, 'select', obj);
	for (i in elems) {
		if (emptyValueMask.test(elems[i].value)) {
			elems[i].disabled = 'disabled';
		}
	}
	return true;
}
